<?php
namespace app\http\behavior;
use app\backstage\classes\helper\JwtToken;
use app\backstage\model\ManagerUser;
use think\Db;
use think\Request;
use think\Response;
class LogRun
{
    public function run(Request $request, Response $params)
    {
        // 行为逻辑
        if($request->post()){

            $access_token = cookie('access_token');

            if($access_token){

                $result = JwtToken::parseToken($access_token);
                if ($result['status']) {

                    $AdminInfo = ManagerUser::get($result['uid']);
                    $request_param  = $request->isPost() ? $request->post() : $request->get();
                    $response       = $params->getData();

                    //$data['status']         = $response['code'] == 100 ? 1 : 0;
                    $data['request_mode']   = $request->isPost() ? 'POST' : 'GET';
                    $data['request_param']  = json_encode($request_param,JSON_UNESCAPED_UNICODE);
                    $data['response']       = json_encode($response,JSON_UNESCAPED_UNICODE);
                    $data['uid']            = $AdminInfo->id;
                    $data['account']        = $AdminInfo->account;
                    $data['last_login_ip']  = $request->ip();

                    $data['system_module']  = strtolower($request->controller());
                    $data['operation_type'] = strtolower($request->action());
                    $data['path']           = 'backstage/'.$data['system_module'].'/'.$data['operation_type'];

                    Db::table('operation_log')->insert($data);
                }

            }

        }

    }
}